var http = require('../../utils/http.js')

var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listbox:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        http.httppost('InventoryData/FindItemsByWh', { wh: options.code},function(res){
            wx.hideToast();
            var objtemp = {}, tempArr = [];
            res.map(item=>{
                var obj = {};
                item.QtyReal = 0;
                item.ishas = true;
                obj.Color = item.Color;
                obj.ItemID = item.ItemID;
                obj.Size = item.Size;
                obj.Wh = item.Wh;
                tempArr.push(obj);
            })
            objtemp.list = res;
            objtemp.code = options.code;
            objtemp.tempArr = tempArr;
            that.data.listbox.push(objtemp);
            that.setData({
                listbox: that.data.listbox
            })
        },'POST')
    },

    made: function () {
        this.scan()
    },
    scan: function () {
        var that = this;
        wx.scanCode({
            onlyFromCamera: true,
            success: (res1) => {
                if (res1.result.length ==16){

                    http.httppost('InventoryData/ScanResult?str=' + res1.result, {}, res => {
                        wx.hideToast();
                        var flagout = false;
                        for (let j = 0; j < that.data.listbox.length;j++){
                            for (let i = 0; i < that.data.listbox[j].tempArr.length; i++) {
                                if (that.data.listbox[j].tempArr[i].Color == res.Color && that.data.listbox[j].tempArr[i].ItemID == res.ItemID && that.data.listbox[j].tempArr[i].Size == res.Size) {
                                    flagout = true;
                                    if (that.data.listbox[j].list[i].QtyReal >= that.data.listbox[j].list[i].Qty) {
                                        wx.vibrateLong();
                                    } else {
                                        that.data.listbox[j].list[i].QtyReal++;
                                    }
                                    break;
                                }
                            }
                            if (flagout){
                                break;
                            }
               


                        }
                        if (!flagout){
                            wx.vibrateLong();
                            res.ishas = false;
                            var idx = that.data.listbox.length - 1;
                            that.data.listbox[idx].list.push(res)
                        }
                        that.setData({
                            listbox: that.data.listbox
                        })
                        

                    }, 'GET')
                }else{
                    http.httppost('InventoryData/FindItemsByWh', { wh: res1.result }, function (res) {
                        
                        wx.hideToast();
                        var objtemp = {}, tempArr = [];
                        res.map(item => {
                            var obj = {};
                            item.QtyReal = 0;
                            item.ishas = true;
                            obj.Color = item.Color;
                            obj.ItemID = item.ItemID;
                            obj.Size = item.Size;
                            obj.Wh = item.Wh;
                            tempArr.push(obj);
                        })
                        
                        objtemp.list = res;
                        objtemp.code = res1.result;
                        objtemp.tempArr = tempArr;

                        

                        that.data.listbox.push(objtemp);
                        that.setData({
                            listbox: that.data.listbox
                        })
                    }, 'POST')
                } 
                that.scan();   
            }
        })
    },
    confirme:function(){
        var that = this,jsonstr =[];
        that.data.listbox.map(item1=>{
            item1.list.map(item=>{
                var obj = {};
                if (item.ishas && item.QtyReal > 0) {
                    obj.AgentItemID = item.ItemID;
                    obj.Color = item.Color;
                    obj.Size = item.Size;
                    obj.Wh = item.Wh;
                    obj.Qty = item.QtyReal;
                    jsonstr.push(obj)
                }
            })
        })
        if (jsonstr.length<=0){
            wx.showModal({
                title: '',
                content: '请先扫描商品',
            })
        }else{
            wx.showModal({
                title: '',
                content: '是否提交',
                success:function(res){
                    if (res.confirm){
                        http.httppost('InventoryData/PandianData', { jsonstr: JSON.stringify(jsonstr), typeid: 0 }, function (res) {
                            wx.reLaunch({
                                url: '/pages/index/index'
                            })
                        })
                    }
                }
            })    
        }
    }
})